package main

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"log"
	"zk3/api"
	"zk3/api/handler"
	"zk3/inits"
	"zk3/internal/queue"
)

func main() {
	inits.MysqlInit()
	inits.InitRabbitMq()

	r := gin.Default()

	api.Router(r)

	go GetSend()

	r.Run(":8181") // 监听并在 0.0.0.0:8080 上启动服务
}

func GetSend() {

	inits.InitRabbitMq()

	msgs := queue.PublishSub("send_group")

	forever := make(chan bool)
	//启用协程处理消息
	go func() {
		for d := range msgs {
			//消息逻辑处理，可以自行设计逻辑
			log.Printf("Received a message: %s", d.Body)

			var data string
			json.Unmarshal(d.Body, &data)

			for _, onlineConn := range handler.OnlineUser {

				handler.WSRespSuccess(onlineConn, data)
			}
		}
	}()

	log.Printf(" [*] Waiting for messages. To exit press CTRL+C")
	<-forever

}
